﻿using System;
using System.Linq;
using System.Windows.Media;

namespace AgFractal.Coloring
{
    public class BasicColoring : AbstractElement, IColoring, ISupportGradient
    {
        public BasicColoring()
        {
            Name = "Basic";
            ID = new Guid("eb771b6a-6b3e-4640-b1d7-f8b5bb305c52");
        }

        #region IColoring Members

        public Color GetColor(Complex point, int iteration)
        {
            int idx = iteration * 80 / 16;
            idx = idx % gradientColorsLength;
            return gradientColors[idx];
        }

        #endregion


        #region ISupportGradient Members

        Color[] gradientColors;
        int gradientColorsLength;
        private Gradient _Gradient;
        public Gradient Gradient
        {
            get
            {
                return _Gradient;
            }
            set
            {
                if (_Gradient == value)
                    return;
                if (_Gradient != null)
                    _Gradient.GradientChanged -= GradientChanged;
                value.GradientChanged += GradientChanged;
                _Gradient = value;
                UpdateGradientColors();
            }
        }

        void GradientChanged(object sender, EventArgs e)
        {
            UpdateGradientColors();
        }

        void UpdateGradientColors()
        {
            gradientColors = Gradient.Colors.ToArray();
            gradientColorsLength = gradientColors.Length;
        }

        #endregion
    }
}